-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
    print("----------------------------------------")
    print("LUA ERROR: " .. tostring(msg) .. "\n")
    print(debug.traceback())
    print("----------------------------------------")
end

-- global objects
ppl = pplData:sharedPplData()
director = CCDirector:sharedDirector()
require "pplConfig"
require "SeqRun"
require "ui/play"
require "ui/result"
require "ui/ladder"
require "ui/welcome"

function StartGame() play:Start(pplCfg.init_row0_y) end
welcome:Input(StartGame)
play:Input(function() result:Show() end)
result:Input(StartGame, function()ladder:Show()end)
ladder:Input(StartGame)

sceneGame = nil
function GetMainScene()
    return sceneGame;
end

local function main()    
    local cclog = function(...)
        print(string.format(...))
    end

--    local function onTouchBegan(x, y)
--        PlayLayer:onTouchBegan(x, y);
--        -- CCTOUCHBEGAN event must return true
--        return true;
--    end
--    local function onTouchMoved(x, y)
--        PlayLayer:onTouchMoved(x, y);
--    end
--    local function onTouchEnded(x, y)
--        --print("moved 11")
--        PlayLayer:onTouchEnded(x, y);
--    end
--    local function onTouch(eventType, x, y)
--        if eventType == CCTOUCHBEGAN then
--            return onTouchBegan(x, y)
--        elseif eventType == CCTOUCHMOVED then
--        --print("moved 11")
--            return onTouchMoved(x, y)
--        else
--            return onTouchEnded(x, y)
--        end
--    end

--    local function funPlayAgain()
--        print("yes you did it")
--    end
--    local function createLayer()
--        local layer = CCLayer:create()
--        layer:setTouchEnabled(true)
--        layer:registerScriptTouchHandler(onTouch)
--            
--            local bg = CCSprite:create("result.png")
--            layer:addChild(bg)
--
--            local button = CCMenuItemImage:create("playAgain1.png", "playAgain2.png")
--            button:registerScriptTapHandler(PlayAgain)
--
--            local menu = CCMenu:create()
--            menu:addChild(button)
--            layer:addChild(menu)
--
--            layer:setPosition(pplCfg.RESOLUTION.width/2, pplCfg.RESOLUTION.height/2)
--        return layer
--    end

    --local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("background.mp3")
    --SimpleAudioEngine:sharedEngine():playBackgroundMusic(bgMusicPath, true)

    --CCEGLView:sharedOpenGLView():setFrameSize(WINDOW_SIZE.width, WINDOW_SIZE.height)
    --director:getOpenGLView():setFrameSize(pplCfg.WINDOW_SIZE.width, pplCfg.WINDOW_SIZE.height)
    
--    local function Start()
--        PlayLayer:Start(layer, pplCfg.init_row0_y);
--        --LadderLayer:Start(layer)
--    end

    sceneGame = CCScene:create()
    --PlayLayer.parent = sceneGame;
    --PlayLayer:Start(pplCfg.init_row0_y);
    welcome:Show()
    director:runWithScene(sceneGame)

    local function Run()
        play:Run()
    end

    director:getScheduler():scheduleScriptFunc(Run, 0, false)
end

xpcall(main, __G__TRACKBACK__)
